package com.ruoyi.generator.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.generator.mapper.FromCrudTableConfigMapper;
import com.ruoyi.generator.domain.FromCrudTableConfig;
import com.ruoyi.generator.service.IFromCrudTableConfigService;

/**
 * 表单设计__业务数据风格配置Service业务层处理
 *
 * @author ZGL
 * @date 2025-03-13
 */
@Service
public class FromCrudTableConfigServiceImpl implements IFromCrudTableConfigService
{
    @Autowired
    private FromCrudTableConfigMapper fromCrudTableConfigMapper;

    /**
     * 查询表单设计__业务数据风格配置
     *
     * @param id 表单设计__业务数据风格配置主键
     * @return 表单设计__业务数据风格配置
     */
    @Override
    public FromCrudTableConfig selectFromCrudTableConfigById(Long id)
    {
        return fromCrudTableConfigMapper.selectFromCrudTableConfigById(id);
    }

    /**
     * 查询表单设计__业务数据风格配置列表
     *
     * @param fromCrudTableConfig 表单设计__业务数据风格配置
     * @return 表单设计__业务数据风格配置
     */
    @Override
    public List<FromCrudTableConfig> selectFromCrudTableConfigList(FromCrudTableConfig fromCrudTableConfig)
    {
        return fromCrudTableConfigMapper.selectFromCrudTableConfigList(fromCrudTableConfig);
    }

    /**
     * 新增表单设计__业务数据风格配置
     *
     * @param fromCrudTableConfig 表单设计__业务数据风格配置
     * @return 结果
     */
    @Override
    public int insertFromCrudTableConfig(FromCrudTableConfig fromCrudTableConfig)
    {
        fromCrudTableConfig.setCreateTime(DateUtils.getNowDate());
        return fromCrudTableConfigMapper.insertFromCrudTableConfig(fromCrudTableConfig);
    }

    /**
     * 修改表单设计__业务数据风格配置
     *
     * @param fromCrudTableConfig 表单设计__业务数据风格配置
     * @return 结果
     */
    @Override
    public int updateFromCrudTableConfig(FromCrudTableConfig fromCrudTableConfig)
    {
        fromCrudTableConfig.setUpdateTime(DateUtils.getNowDate());
        return fromCrudTableConfigMapper.updateFromCrudTableConfig(fromCrudTableConfig);
    }

    /**
     * 批量删除表单设计__业务数据风格配置
     *
     * @param ids 需要删除的表单设计__业务数据风格配置主键
     * @return 结果
     */
    @Override
    public int deleteFromCrudTableConfigByIds(Long[] ids)
    {
        return fromCrudTableConfigMapper.deleteFromCrudTableConfigByIds(ids);
    }

    /**
     * 删除表单设计__业务数据风格配置信息
     *
     * @param id 表单设计__业务数据风格配置主键
     * @return 结果
     */
    @Override
    public int deleteFromCrudTableConfigById(Long id)
    {
        return fromCrudTableConfigMapper.deleteFromCrudTableConfigById(id);
    }

    public JSONObject selectFromCrudTableConfigByBuildKey(String buildKey){
        FromCrudTableConfig fromCrudTableConfig = fromCrudTableConfigMapper.selectFromCrudTableConfigByBuildKey(buildKey);
        JSONObject config = null;
        if(fromCrudTableConfig!=null && StringUtils.isNotEmptyIncludeNull(fromCrudTableConfig.getConfig())){
            config = JSONObject.parseObject(fromCrudTableConfig.getConfig());
        }
        return config;
    }

    /**
     * 更新数据表风格配置
     */
    @Override
    public int refreshTableConfig(String formBuildKey, Map<String,Object> fromCrudTableConfig){

        if(StringUtils.isEmptyIncludeNull(formBuildKey)){
            return 0;
        }

        //删除之前的配置信息
        fromCrudTableConfigMapper.deleteFromCrudTableConfigByBuildKey(formBuildKey);

        //保存当前的配置信息
        if(fromCrudTableConfig == null){
            fromCrudTableConfig = new HashMap<>();
            //表格样式编码  组件唯一标识
            fromCrudTableConfig.put("moduleType","Default");
            //组件描述
            fromCrudTableConfig.put("label","默认样式");
            //带斑马纹表格
            fromCrudTableConfig.put("stripe","false");
            //带边框表格
            fromCrudTableConfig.put("border","true");
            //固定操作按钮
            fromCrudTableConfig.put("fixedBtnColumn","right");
        }
        FromCrudTableConfig data=new FromCrudTableConfig();
        data.setBuildKey(formBuildKey);
        data.setConfig(JSONObject.toJSONString(fromCrudTableConfig));
        return fromCrudTableConfigMapper.insertFromCrudTableConfig(data);
    }
}
